package com.mic.yzzn.plc.common.instructions.request;

import java.util.ArrayList;

import com.mic.yzzn.plc.common.ins.datastructure.PLCContactorNumber;
import com.mic.yzzn.plc.common.instructions.basic.PLCRequestInstruction;

public class PLCMultipleContactorInstruction extends PLCRequestInstruction
{
	/** 触点的数量 */
	protected int count = 0;
	/** 触点代码数组 */
	protected ArrayList<String> contactorCodes = new ArrayList<String>();
	/** 触点编号数组 */
	protected ArrayList<PLCContactorNumber> contactorNumbers = new ArrayList<PLCContactorNumber>();
	
	// Getter
	public ArrayList<String> getContactorCodes()
	{
		return contactorCodes;
	}
	
	public ArrayList<PLCContactorNumber> getContactorNumbers()
	{
		return contactorNumbers;
	}

	public int getCount() {
		return count;
	}

	/**
	 * 触点数量set方法
	 * @param count
	 */
	public void setCount(int count)
	{
		if (count < 0 || count > 8)
		{
			throw new IllegalArgumentException("count must in the range of 0~8, but the count is " + count);
		}
		this.count = count;
	}
	
	/**
	 * 触点代码数组set方法
	 * @param contactorCodes
	 */
	public void setContactorCodes(ArrayList<String> contactorCodes) throws IllegalArgumentException
	{
		if (contactorCodes.size() != count)
		{
			throw new IllegalArgumentException("contactorCodes' size must equal to count, but the count is " + count);
		}
		this.contactorCodes = contactorCodes;
	}
	
	/**
	 * 触点编号数组set方法
	 * @param contactorNumbers
	 */
	public void setContactorNumbers(ArrayList<PLCContactorNumber> contactorNumbers)
	{
		if (contactorNumbers.size() != count)
		{
			throw new IllegalArgumentException("contactorNumbers' size must equal to count, but the count is " + count);
		}
		this.contactorNumbers = contactorNumbers;
	}

	/**
	 * 计算并返回每一位的异或
	 */
	@Override
	protected int getXORByBit()
	{
        int i = 0;
        int result = 0;
        for (i = 0; i < contactorCodes.size(); ++i)
        {
        	String code = contactorCodes.get(i);
        	for (int j = 0; j < code.length(); ++j)
        	{
        		result ^= code.charAt(j);
        	}
        }
        
        for (i = 0; i < contactorNumbers.size(); ++i)
        {	
        	result ^= contactorNumbers.get(i).getXORByBit();
        }
        
        String countString = String.valueOf(count);
        for (i = 0; i < countString.length(); ++i)
        {	
        	result ^= countString.charAt(i);
        }
        
        return super.getXORByBit() ^ result;
	}
	
    /**
     * 转化成字符串
     */
    @Override
    public String toString()
    {
        // 拼接指令字符串
        StringBuffer buffer = new StringBuffer();
        buffer.append(instructionHeader);
        buffer.append(locationNumber_high);
        buffer.append(locationNumber_low);
        buffer.append(instructionTypeCode);
        buffer.append(requestCode);
        buffer.append(count);
        for (int i = 0; i < count; ++i)
        {
        	buffer.append(contactorCodes.get(i) + contactorNumbers.get(i));
        }
        buffer.append(getBBC_high());
        buffer.append(getBBC_low());
        buffer.append(instructionEnd);

        return buffer.toString();
    }
}
